import re
from posixpath import normpath
from urllib.parse import urljoin, urlparse, urlunparse

import chardet

from ScrapyObject.items import VideoBean


# 得到返回结果
def get_data(response):
    return response.body.decode(chardet.detect(response.body)['encoding'] or 'utf-8', 'ignore')


def get_video_item(id, tags='', url='', name='', pUrl='', vUrl=''):
    """ 获取视频数据
    """
    item = VideoBean()
    item['id'] = id
    item['tags'] = tags
    item['url'] = url
    item['name'] = name
    item['pUrl'] = pUrl
    item['vUrl'] = vUrl
    return item


# url拼接
def split_joint(base, url):
    url1 = urljoin(base, url)
    arr = urlparse(url1)
    path = normpath(arr[2])
    return urlunparse((arr.scheme, arr.netloc, path, arr.params, arr.query, arr.fragment))


# 从传入的字符串中取出所有url并返回
def get_url(content):
    return re.findall(r"(?<=href=\").+?(?=\")|(?<=href=\').+?(?=\')", content)


def format_url_one(video_url):
    """ 格式化url
    """
    return video_url.replace("\\/", "/")


def get_video_url_one(content):
    """ 获取视频地址
    """
    return re.findall(r'http.*?\.M3U8|http.*?\.MP4|http.*?\.WMV|http.*?\.MOV|http.*?\.AVI|http.*?\.MKV|http.*?\.FLV|http.*?\.RMVB|http.*?\.3GP', content, re.IGNORECASE)


def get_video_url_two(content):
    """ 获取视频地址
    """
    return re.findall(r'http[^\'"]*?\.M3U8|http[^\'"]*?\.MP4|http[^\'"]*?\.WMV|http[^\'"]*?\.MOV|http[^\'"]*?\.AVI|http[^\'"]*?\.MKV|http[^\'"]*?\.FLV|http[^\'"]*?\.RMVB|http[^\'"]*?\.3GP', content, re.IGNORECASE)


def get_video_url_three(content):
    """ 获取视频地址
    """
    return re.findall(r'https?:[^\'",]+?\.(?:mp4|webm|ogg|mov|avi|flv|mkv|m3u8|mpd|wmv|rmvb|3gp)', content, re.IGNORECASE)


def get_video_url_four(content):
    '''
    获取视频地址
    '''
    video_pattern = re.compile(r'https?://[^\s"\']+?\.(?:mp4|webm|ogg|mov|avi|flv|mkv|m3u8|wmv|rmvb|3gp|MP4|WEBM|OGG|MOV|AVI|FLV|MKV|M3U8|WMV|RMVB|3GP)(?:\?[^\s"\']*)?', re.IGNORECASE)
    src_pattern = re.compile(r'<(?:video|source|iframe)[^>]+(?:src|data-src)=[\'"]([^\'"]+)[\'"]', re.IGNORECASE)
    video_links = video_pattern.findall(content)
    src_links = src_pattern.findall(content)
    return list(set(video_links + src_links))
